package com.szmmsoft.mp.common.until;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTPayload;
import com.szmmsoft.mp.common.exception.BusinessException;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Description of the class.
 *
 * @author ethan
 * @version 1.0.0
 * @since 2024-10-16
 */
public class JWTUtil extends cn.hutool.jwt.JWTUtil {
    /**
     * 秘钥
     */
    private final static String JWT_KEY = "hhduh;;apqlwdad]]adjaid";
    /**
     * 过期时间
     */
   // public final static Long JWT_EXPIRE_TIME = 15 * 24 * 60 * 60 * 1000L;
    public final static Long JWT_EXPIRE_TIME = 2000*60L;
    /**
     * uid
     */
    public static final String JWT_UID = "uid";


    /**
     * 续期时间提前1天
     */
    //public final static Long JWT_HEAD_REFRESH_TIME = 1*3600*1000L;
    public final static Long JWT_HEAD_REFRESH_TIME = 1000*60L;
    /**
     * 获取token
     *
     * @param userId
     * @return
     */
    public static String token(Long userId) {
        Map<String, Object> map = new ConcurrentHashMap<>();
        map.put(JWT_UID, userId.toString());
        //加个时间不然每次的都一样
        map.put("expireTime", System.currentTimeMillis()+JWT_HEAD_REFRESH_TIME+"");
        return createToken(map, JWT_KEY.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 获取jwt信息
     *
     * @param token
     * @return
     */
    public static JWTPayload getPayLoad(String token) {
        try {
            return parseToken(token).getPayload();
        } catch (Exception e) {
            throw new BusinessException("token不正常,解析异常");
        }
    }

    public static void main(String[] args) {
        System.out.println(token(1L));
        String token = token(1L);


    }
}
